package com.sinosoft.common.manage.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinosoft.common.manage.domain.GoodsUnderIssueAnswer;
import com.sinosoft.common.manage.domain.IssueInfoAnswer;
import com.sinosoft.common.manage.mapper.GoodsUnderIssueAnswerMapper;
import com.sinosoft.common.manage.service.GoodsUnderIssueAnswerService;
import com.sinosoft.common.manage.vo.GoodsUnderDiseaseInfoVO;
import com.sinosoft.common.manage.vo.GoodsUnderIssueAnswerVO;
import com.sinosoft.common.manage.vo.IssueInfoAnswerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Yeung
* @description 针对表【goods_under_issue_answer(核保问题答案选项信息表)】的数据库操作Service实现
* @createDate 2024-04-24 10:15:30
*/
@Service
@Slf4j
public class GoodsUnderIssueAnswerServiceImpl extends ServiceImpl<GoodsUnderIssueAnswerMapper, GoodsUnderIssueAnswer>
    implements GoodsUnderIssueAnswerService{

    @Resource
    private GoodsUnderIssueAnswerMapper goodsUnderIssueAnswerMapper;

    @Override
    public List<IssueInfoAnswerVO> queryByIssueId(List<GoodsUnderDiseaseInfoVO> goodsUnderDiseaseInfoVOList) {
        log.info("com.sinosoft.common.manage.service.impl.GoodsUnderIssueAnswerServiceImpl.queryByIssueId 入参={}", JSON.toJSONString(goodsUnderDiseaseInfoVOList));
        try {
            List<IssueInfoAnswerVO> issueInfoAnswerVos = new ArrayList<>();
            List<Integer> issueIdList = new ArrayList<>();
            List<GoodsUnderIssueAnswerVO> goodsUnderIssueAnswerVOList = new ArrayList<>();
            List<String> issues = new ArrayList<>();
            for (GoodsUnderDiseaseInfoVO goodsUnderDiseaseInfoVO : goodsUnderDiseaseInfoVOList) {
                issues.add(goodsUnderDiseaseInfoVO.getIssueId());
            }

            List<IssueInfoAnswer> issueInfoAnswers = goodsUnderIssueAnswerMapper.queryByIssueId(issues);
            //组装问题和问题答案列表
            for (IssueInfoAnswer issueInfoAnswer : issueInfoAnswers) {
                issueIdList.add(issueInfoAnswer.getIssueId());
                GoodsUnderIssueAnswerVO goodsUnderIssueAnswerVO = new GoodsUnderIssueAnswerVO();
                goodsUnderIssueAnswerVO.setIssueId(issueInfoAnswer.getIssueId());
                goodsUnderIssueAnswerVO.setParentId(issueInfoAnswer.getParentId());
                goodsUnderIssueAnswerVO.setIssueCode(issueInfoAnswer.getIssueCode());
                goodsUnderIssueAnswerVO.setIssueText(issueInfoAnswer.getIssueText());
                goodsUnderIssueAnswerVO.setAnswerId(issueInfoAnswer.getAnswerId());
                goodsUnderIssueAnswerVO.setAnswerSel(issueInfoAnswer.getAnswerSel());
                goodsUnderIssueAnswerVO.setNextIssueId(issueInfoAnswer.getNextIssueId());
                goodsUnderIssueAnswerVOList.add(goodsUnderIssueAnswerVO);
            }
            //问题编号列表去重
            List<Integer> issueIds= issueIdList.stream().distinct().collect(Collectors.toList());
            //组装问题答案关联列表
            for (Integer s : issueIds) {
                IssueInfoAnswerVO issueInfoAnswerVO = new IssueInfoAnswerVO();
                List<GoodsUnderIssueAnswerVO> goodsUnderIssueAnswerVos = new ArrayList<>();
                String issueText ="";
                String parentId = "";
                String issueCode = "";
                for (GoodsUnderIssueAnswerVO goodsUnderIssueAnswerVO : goodsUnderIssueAnswerVOList) {
                    if (s.equals(goodsUnderIssueAnswerVO.getIssueId())){
                        issueText = goodsUnderIssueAnswerVO.getIssueText();
                        goodsUnderIssueAnswerVos.add(goodsUnderIssueAnswerVO);
                        parentId = goodsUnderIssueAnswerVO.getParentId().toString();
                        issueCode = goodsUnderIssueAnswerVO.getIssueCode();
                    }
                }
                for (GoodsUnderDiseaseInfoVO goodsUnderDiseaseInfoVO : goodsUnderDiseaseInfoVOList) {
                    if (s.toString().equals(goodsUnderDiseaseInfoVO.getIssueId())){
                        issueInfoAnswerVO.setSonDiseaseName(goodsUnderDiseaseInfoVO.getSonDiseaseName());
                    }
                }
                issueInfoAnswerVO.setIssueId(s);
                issueInfoAnswerVO.setParentId(Integer.parseInt(parentId));
                issueInfoAnswerVO.setIssueCode(issueCode);
                issueInfoAnswerVO.setIssueText(issueText);
                issueInfoAnswerVO.setGoodsUnderIssueAnswerVOList(goodsUnderIssueAnswerVos);
                issueInfoAnswerVos.add(issueInfoAnswerVO);
            }
            log.info("com.sinosoft.common.manage.service.impl.GoodsUnderIssueAnswerServiceImpl.queryByIssueId 返回={}", JSON.toJSONString(issueInfoAnswerVos));
            return issueInfoAnswerVos;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}




